package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.AlipaySignature;
import com.auth0.jwt.JWT;
import com.qf.entity.TlHotelOrder;
import com.qf.service.HotelOrderService;
import com.qf.utils.AliPayTools2;
import com.qf.utils.PayMap;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hotelorder")

public class HotelOrderController {
    @Autowired
    HotelOrderService hotelOrderService;
    @Autowired
    AliPayTools2 aliPayTools2;
    private String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm4Zclwnjfi0mgW6gg+oz21lBPKs+Ovin06FFb8nRFlmcpZboMYdtfuzGPOtSlo4m6kzpXJRyZqA3hFAO4MgUwGR1dfYc/QgCo+/2nq0HqgObWPJYi2bhBPZwhFO3xB5KRPd8KwlgqETwFfs1MGABd32Jvgh1YxKjCXF0TqFKOyosICPQPFCVzLBVCssCccznrdBk/K+kzRrgT2QmJv9Th0MRQ7ZjH/MGa0RqD+UW/z4dIKYodEwTmx1oHFjI4CQNJa2OWxPs7OJtu75H3E/ZaUOe/Cv79MrbfpfxbpDF7jEf3Dbsa2F0cs6sHsDmDLovsLYGZdb59URWmgcB/TCLdwIDAQAB";
    @GetMapping("/findallorder")//查询该用户下所有的订单
    public ResultMsg findUserHotelorder(String uuid){
        return hotelOrderService.findUserHotelOrder(uuid);
    }
    @PostMapping("/updateorder")//修改订单
    public ResultMsg update(@RequestBody TlHotelOrder tlHotelOrder){
        return hotelOrderService.update(tlHotelOrder);

    }
    @DeleteMapping("/deletepayorder")//删除已经支付的订单
   public ResultMsg deleteHotrlorder(String horderId){//删除订单
        return hotelOrderService.deletepayorder(horderId);
   }

   @DeleteMapping("/deletenotpayorder")//取消预订的订单
   public ResultMsg deletenotpayorder(String horderId){
       return hotelOrderService.deletenopayorder(horderId);
   }

   @GetMapping("/inserorder")//添加预订和支付生成的订单
    public ResultMsg insertHotelOrder(Integer hotelId, String bedname, BigDecimal Price, HttpServletRequest request){//添加订单
        return hotelOrderService.insertHotelOrder(hotelId,bedname,Price,request);
   }

    @GetMapping("/selectorderitem")//查询单个已支付订单详情
    public ResultMsg selectorderitem(String horderId){
        return hotelOrderService.findOneOrder(horderId);
    }

    @PostMapping("/tuimoney")
    public ResultMsg tuimoney(@RequestBody Map map){
        String horderId = (String) map.get("horderId");
        Integer horderPrcie = (Integer) map.get("horderPrice");
        String s = aliPayTools2.tuiKuan(horderId, horderPrcie);
        if (s.equals("Y"))
                 return hotelOrderService.tuikuan(horderId);
        return ResultMsg.ERROR(999,"退款失败");
    }

    @PostMapping("/phonehotelordertopay")//手机端支付
    public ResultMsg phonetopay(@RequestBody Map map){
        String horderId = (String) map.get("horderId");
        Integer horderPrcie = (Integer) map.get("horderPrice");
        String order = aliPayTools2.pay(horderId, "酒店订单", horderPrcie,"/hotelorder/notify");

        return ResultMsg.SUCCESS(order);
    }

    @PostMapping("/hotelordertopay")
    public ResultMsg topay(@RequestBody Map map){
        String horderId = (String) map.get("horderId");
        Integer horderPrcie = (Integer) map.get("horderPrice");
        String order = aliPayTools2.saoma(horderId, "酒店订单", horderPrcie,"/hotelorder/notify");

        return ResultMsg.SUCCESS(order);
    }

    //异步回调
    @PostMapping("/notify/{gid}")
    public ResultMsg goodsnotify(@PathVariable("gid") String gid, HttpServletRequest request)throws Exception{
        //二次验签：request的值转为map
        Map<String, String> paramsMap = PayMap.converToMap(request);
        //将异步通知中收到的所有参数都存放到 map 中
        boolean  signVerified = AlipaySignature.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, "utf-8", "RSA2");  //调用SDK验证签名
        if (signVerified){
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure

            String status = paramsMap.get("trade_status");
            if(status.equals("TRADE_SUCCESS")){
                //订单状态的修改
                ResultMsg oneOrder = hotelOrderService.findOneOrder(gid);
                TlHotelOrder order = (TlHotelOrder) oneOrder.getData();
                order.setHorderFlag(1);
                hotelOrderService.update(order);
            }
            return ResultMsg.SUCCESS();
        } /*else {
            // TODO 验签失败则记录异常日志，并在response中返回failure.
        }
        return null;*/
        throw new RuntimeException("支付验签失败");
    }

    @GetMapping("/zero")
    public ResultMsg zero(String uuid){
        return hotelOrderService.zero(uuid);
    }
    @GetMapping("/one")
    public ResultMsg one(String uuid){
        return hotelOrderService.one(uuid);
    }
    @GetMapping("/two")
    public ResultMsg two(String uuid){
        return hotelOrderService.two(uuid);
    }
}
